import 'package:backend.auth/infrastructure/controllers/email_verification_during_signing_up_controller/controller.dart';
import 'package:backend.auth/infrastructure/controllers/signing_in_controller/controller.dart';
import 'package:backend.auth/infrastructure/controllers/signing_out_all_controller/controller.dart';
import 'package:backend.auth/infrastructure/controllers/signing_out_controller/controller.dart';
import 'package:backend.auth/infrastructure/controllers/signing_up_controller/controller.dart';
import 'package:backend.auth/infrastructure/controllers/token_refreshment_controller/controller.dart';
import 'package:backend.auth/injection_container.dart' as auth_di;
import 'package:backend.core/core/infrastructure/middlewares/forbid_admin_authorization.dart';
import 'package:backend.core/core/infrastructure/middlewares/require_admin_authorization.dart';
import 'package:backend.core/core/infrastructure/middlewares/require_authorization.dart';
import 'package:backend.core/core/infrastructure/middlewares/require_end_user_authorization.dart';
import 'package:backend.core/injection_container.dart' as core_di;
import 'package:backend.lexical_item_images/infrastructure/controllers/lexical_item_image_change_controller/controller.dart';
import 'package:backend.lexical_item_images/infrastructure/controllers/lexical_item_image_deletion_controller/controller.dart';
import 'package:backend.lexical_item_images/injection_container.dart' as lexical_item_image_di;
import 'package:backend.purchases/infrastructure/controllers/app_store_api_notification_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/google_play_api_notification_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/google_play_new_purchased_item_sync_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/order_placing_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/purchased_item_obtainment_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/purchased_item_restoration_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/web_purchase_item_getting_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/web_purchased_subscription_auto_renewal_enablement_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/web_purchased_subscription_cancellation_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/web_saved_payment_method_getting_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/web_saved_payment_method_removal_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/web_transaction_history_getting_controller/controller.dart';
import 'package:backend.purchases/infrastructure/controllers/yookassa_api_notification_controller/controller.dart';
import 'package:backend.purchases/injection_container.dart' as purchases_di;
import 'package:backend.user_profile/infrastructure/controllers/email_change_controller/controller.dart';
import 'package:backend.user_profile/infrastructure/controllers/email_verification_during_email_change_controller/controller.dart';
import 'package:backend.user_profile/infrastructure/controllers/password_change_controller/controller.dart';
import 'package:backend.user_profile/infrastructure/controllers/password_reset_controller/controller.dart';
import 'package:backend.user_profile/infrastructure/controllers/password_reset_request_controller/controller.dart';
import 'package:backend.user_profile/infrastructure/controllers/user_deletion_controller/controller.dart';
import 'package:backend.user_profile/infrastructure/controllers/user_getting_controller/controller.dart';
import 'package:backend.user_profile/injection_container.dart' as user_profile_di;
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

final router = Router()
// auth
  ..post('/sign-in', _signingInHandler)
  ..post('/sign-up', _signingUpHandler)
  ..post('/sign-up/verify-email', _emailVerificationDuringSigningUpHandler)
  ..post('/sign-out', _signingOutHandler)
  ..post('/sign-out-all', _signingOutAllHandler)
  ..post('/refresh-token', _tokenRefreshmentHandler)
// lexical_item_images
  ..put('/change-lexical-item-image', _lexicalItemImageChangeHandler)
  ..delete('/delete-lexical-item-image', _lexicalItemImageDeletionHandler)
// purchases
  ..get('/web-purchase-items', _webPurchaseItemGettingHandler)
  ..get('/web-saved-payment-methods', _webSavedPaymentMethodGettingHandler)
  ..get('/web-transaction-history', _webTransactionHistoryGettingHandler)
  ..post('/restore-purchased-items', _purchasedItemRestorationHandler)
  ..post('/obtain-purchased-items', _purchasedItemObtainmentHandler)
  ..post('/sync-google-play-new-purchased-item', _googlePlayNewPurchasedItemSyncHandler)
  ..post('/place-order', _orderPlacingHandler)
  ..post('/cancel-web-purchased-subscription', _webPurchasedSubscriptionCancellationHandler)
  ..post('/enable-web-purchased-subscription-auto-renewal',
      _webPurchasedSubscriptionAutoRenewalEnablementHandler)
  ..post('/remove-web-saved-payment-method', _webSavedPaymentMethodRemovalHandler)
  ..post('/yookassa-notification', _yookassaApiNotificationHandler)
  ..post('/app-store-notification', _appStoreApiNotificationHandler)
  ..post('/google-play-notification', _googlePlayApiNotificationHandler)
// user_profile
  ..get('/user', _userGettingHandler)
  ..delete('/user', _userDeletionHandler)
  ..post('/change-email/verify-email', _emailVerificationDuringEmailChangeHandler)
  ..post('/request-password-reset', _passwordResetRequestHandler)
  ..put('/change-password', _passwordChangeHandler)
  ..put('/change-email', _emailChangeHandler)
  ..put('/reset-password', _passwordResetHandler);

// middlewares

final _requireAuthorization = core_di.sl<RequireAuthorization>();
final _requireEndUserAuthorization = core_di.sl<RequireEndUserAuthorization>();
final _requireAdminAuthorization = core_di.sl<RequireAdminAuthorization>();
final _forbidAdminAuthorization = core_di.sl<ForbidAdminAuthorization>();

// auth handlers

final _signingInHandler = auth_di.sl<SigningInController>();
final _signingUpHandler = auth_di.sl<SigningUpController>();

final _emailVerificationDuringSigningUpHandler =
    auth_di.sl<EmailVerificationDuringSigningUpController>();

final _signingOutHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(auth_di.sl<SigningOutController>());

final _signingOutAllHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(auth_di.sl<SigningOutAllController>());

final _tokenRefreshmentHandler = auth_di.sl<TokenRefreshmentController>();

// lexical item image handlers

final _lexicalItemImageChangeHandler = const Pipeline()
    .addMiddleware(_requireAdminAuthorization())
    .addHandler(lexical_item_image_di.sl<LexicalItemImageChangeController>());

final _lexicalItemImageDeletionHandler = const Pipeline()
    .addMiddleware(_requireAdminAuthorization())
    .addHandler(lexical_item_image_di.sl<LexicalItemImageDeletionController>());

// purchase handlers

final _purchasedItemRestorationHandler = const Pipeline()
    .addMiddleware(_forbidAdminAuthorization())
    .addHandler(purchases_di.sl<PurchasedItemRestorationController>());

final _purchasedItemObtainmentHandler = const Pipeline()
    .addMiddleware(_forbidAdminAuthorization())
    .addHandler(purchases_di.sl<PurchasedItemObtainmentController>());

final _googlePlayNewPurchasedItemSyncHandler = const Pipeline()
    .addMiddleware(_forbidAdminAuthorization())
    .addHandler(purchases_di.sl<GooglePlayNewPurchasedItemSyncController>());

final _webTransactionHistoryGettingHandler = const Pipeline()
    .addMiddleware(_requireEndUserAuthorization())
    .addHandler(purchases_di.sl<WebTransactionHistoryGettingController>());

final _yookassaApiNotificationHandler =
    const Pipeline().addHandler(purchases_di.sl<YookassaApiNotificationController>());

final _orderPlacingHandler = const Pipeline()
    .addMiddleware(_requireEndUserAuthorization())
    .addHandler(purchases_di.sl<OrderPlacingController>());

final _webPurchaseItemGettingHandler =
    const Pipeline().addHandler(purchases_di.sl<WebPurchaseItemGettingController>());

final _webSavedPaymentMethodGettingHandler = const Pipeline()
    .addMiddleware(_requireEndUserAuthorization())
    .addHandler(purchases_di.sl<WebSavedPaymentMethodGettingController>());

final _appStoreApiNotificationHandler =
    const Pipeline().addHandler(purchases_di.sl<AppStoreApiNotificationController>());

final _googlePlayApiNotificationHandler =
    const Pipeline().addHandler(purchases_di.sl<GooglePlayApiNotificationController>());

final _webPurchasedSubscriptionCancellationHandler = const Pipeline()
    .addMiddleware(_requireEndUserAuthorization())
    .addHandler(purchases_di.sl<WebPurchasedSubscriptionCancellationController>());

final _webPurchasedSubscriptionAutoRenewalEnablementHandler = const Pipeline()
    .addMiddleware(_requireEndUserAuthorization())
    .addHandler(purchases_di.sl<WebPurchasedSubscriptionAutoRenewalEnablementController>());

final _webSavedPaymentMethodRemovalHandler = const Pipeline()
    .addMiddleware(_requireEndUserAuthorization())
    .addHandler(purchases_di.sl<WebSavedPaymentMethodRemovalController>());

// user_profile handlers

final _userGettingHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(user_profile_di.sl<UserGettingController>());

final _userDeletionHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(user_profile_di.sl<UserDeletionController>());

final _passwordChangeHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(user_profile_di.sl<PasswordChangeController>());

final _emailChangeHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(user_profile_di.sl<EmailChangeController>());

final _emailVerificationDuringEmailChangeHandler = const Pipeline()
    .addMiddleware(_requireAuthorization())
    .addHandler(user_profile_di.sl<EmailVerificationDuringEmailChangeController>());

final _passwordResetRequestHandler =
    const Pipeline().addHandler(user_profile_di.sl<PasswordResetRequestController>());

final _passwordResetHandler =
    const Pipeline().addHandler(user_profile_di.sl<PasswordResetController>());
